package com.example.javabase.tools;


import com.example.javabase.entity.WorkTaskRegisterDto;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecursionUtils1 {

    public static void main(String[] args) {

    }

    /**
     * 说明方法描述：将list转为树tree结构
     */
    public List<WorkTaskRegisterDto> useListRecordToTree(List<WorkTaskRegisterDto> allRecords) {
        List<WorkTaskRegisterDto> listParentRecord = new ArrayList<>();
        List<WorkTaskRegisterDto> listNotParentRecord = new ArrayList<>();
        // 第一步：遍历allRecords保存所有数据的uuid用于判断是不是根节点
        Map<String, String> mapAllUuid = new HashMap<>();
        //Map<String, WorkTaskRegisterDto> allRecordMap = new HashMap<>();
        for (WorkTaskRegisterDto record : allRecords) {
            mapAllUuid.put(record.getId(), record.getId());
            //allRecordMap.put(record.getId(), record);
        }
        // 第二步：遍历allRecords找出所有的根节点和非根节点
        if (allRecords.size() > 0) {
            for (WorkTaskRegisterDto record : allRecords) {
                if (StringUtils.isEmpty(record.getParentId())
                        || !mapAllUuid.containsKey(record.getParentId())) {
                    listParentRecord.add(record);
                } else {
                    listNotParentRecord.add(record);
                }
            }
        }

        // 第三步： 递归获取所有子节点
        if (listParentRecord.size() > 0) {
            for (WorkTaskRegisterDto record : listParentRecord) {
                // 添加所有子级
                record.setChildren(this.getTreeChildRecord(listNotParentRecord, record.getId()));
            }
        }
        return listParentRecord;
    }

    /**
     * 说明方法描述：使list转换为树并根据关键字和节点名称过滤
     *
     * @param allRecords   所有节点
     * @param keywords     要过滤的关键字
     * @param filterFields 要过滤的字段
     */
    public List<WorkTaskRegisterDto> useListRecordToTreeByKeywords(List<WorkTaskRegisterDto> allRecords, String keywords, String... filterFields) {
        List<WorkTaskRegisterDto> listRecord = new ArrayList<>();
        Map<String, WorkTaskRegisterDto> allRecordMap = new HashMap<>();
        for (WorkTaskRegisterDto record : allRecords) {
            allRecordMap.put(record.getId(), record);
        }
        // 遍历allRecords找出所有的nodeName和关键字keywords相关的数据
        if (allRecords.size() > 0) {
            for (WorkTaskRegisterDto record : allRecords) {
                // 比较
                if (record.getTaskName().contains(keywords)) {
                    listRecord.add(record);
                }
            }
            /*if (filterFields.length > 1) {
                for (WorkTaskRegisterDto record : allRecords) {
                    for (String field : filterFields) {
                        // 比较
                        if (record.getStr(field).toLowerCase().indexOf(keywords.toLowerCase()) != -1) {
                            listRecord.add(record);
                        }
                    }
                }
            } else {
                for (WorkTaskRegisterDto record : allRecords) {
                    // 比较
                    if (record.getStr(filterFields[0]).toLowerCase().indexOf(keywords.toLowerCase()) != -1) {
                        listRecord.add(record);
                    }
                }
            }*/
        }
        // 查找过滤出来的节点和他们的父节点
        listRecord = this.getSelfAndTheirParentRecord(listRecord, new ArrayList<>(),
                new HashMap<>(), allRecordMap);
        // 将过滤出来的数据变成树tree结构
        listRecord = this.useListRecordToTree(listRecord);
        return listRecord;
    }

    /**
     * 说明方法描述：递归查询子节点
     *
     * @param childList  子节点
     * @param parentUuid 父节点id
     */
    private List<WorkTaskRegisterDto> getTreeChildRecord(List<WorkTaskRegisterDto> childList, String parentUuid) {
        List<WorkTaskRegisterDto> listParentRecord = new ArrayList<>();
        List<WorkTaskRegisterDto> listNotParentRecord = new ArrayList<>();
        // 遍历tmpList，找出所有的根节点和非根节点
        if (childList != null && childList.size() > 0) {
            for (WorkTaskRegisterDto record : childList) {
                // 对比找出父节点
                if (record.getParentId().equals(parentUuid)) {
                    listParentRecord.add(record);
                } else {
                    listNotParentRecord.add(record);
                }

            }
        }
        // 查询子节点
        if (listParentRecord.size() > 0) {
            for (WorkTaskRegisterDto record : listParentRecord) {
                // 递归查询子节点
                record.setChildren(getTreeChildRecord(listNotParentRecord, record.getId()));
            }
        }
        return listParentRecord;
    }

    /**
     * 说明方法描述：递归找出本节点和他们的父节点
     *
     * @param parentList      根据关键字过滤出来的相关节点的父节点
     * @param resultList      返回的过滤出来的节点
     * @param filterRecordMap 已经过滤出来的节点
     * @param allRecordMap    所有节点
     */
    private List<WorkTaskRegisterDto> getSelfAndTheirParentRecord(List<WorkTaskRegisterDto> parentList, List<WorkTaskRegisterDto> resultList,
                                                                  Map<String, WorkTaskRegisterDto> filterRecordMap,
                                                                  Map<String, WorkTaskRegisterDto> allRecordMap) {
        // 当父节点为null或者节点数量为0时返回结果，退出递归
        if (parentList == null || parentList.size() == 0) {
            return resultList;
        }
        // 重新创建父节点集合
        List<WorkTaskRegisterDto> listParentRecord = new ArrayList<>();
        // 遍历已经过滤出来的节点
        for (WorkTaskRegisterDto record : parentList) {

            String uuid = record.getId();
            String parent_uuid = record.getParentId();

            // 如果已经过滤出来的节点不存在则添加到list中
            if (!filterRecordMap.containsKey(uuid)) {
                listParentRecord.add(record);// 添加到父节点中
                filterRecordMap.put(uuid, record);// 添加到已过滤的map中
                allRecordMap.remove(uuid);// 移除集合中相应的元素
                resultList.add(record);// 添加到结果集中
            }

            // 找出本节点的父节点并添加到listParentRecord父节点集合中，并移除集合中相应的元素
            if (StringUtils.isEmpty(parent_uuid)) {
                WorkTaskRegisterDto parentRecord = allRecordMap.get(parent_uuid);
                if (parentRecord != null) {
                    listParentRecord.add(parentRecord);
                    allRecordMap.remove(parent_uuid);
                }
            }

        }
        // 递归调用
        getSelfAndTheirParentRecord(listParentRecord, resultList, filterRecordMap, allRecordMap);
        return resultList;
    }

}
